﻿
using IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class StareDal<T>: IStateDal<T> where T : class

    {
         public StareDal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }



        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAnyns(T t)
        {
            try//异常处理
            {
                 await Context.Set<T>().AddAsync(t);
                 return await Context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async  Task<int> AddRangeAnyns(List<T> list)
        {
            try//异常处理
            {
                await Context.Set<T>().AddRangeAsync(list);
                return await Context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async  Task<int> DeleteAnyns(T t)
        {
            try//异常处理
            {
                Context.Set<T>().Remove(t);
                return await Context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>

        public async  Task<int> DeleteRangeAnyns(List<T> list)
        {
            try//异常处理
            {
                Context.Set<T>().RemoveRange(list);

                return await Context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 延迟
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public IQueryable<T> GetValues()
        {
            try//异常处理
            {
                return Context.Set<T>();

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async  Task<int> UpdateAnyns(T t)
        {
            try//异常处理
            {
                Context.Set<T>().Update(t);

                return await Context.SaveChangesAsync();

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async  Task<int> UpdateRangesAnyns(List<T> list)
        {
            try//异常处理
            {
                Context.Set<T>().UpdateRange(list);

                return await Context.SaveChangesAsync();

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
